// ==========================================================================
// gin-sns自动生成控制器相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:54 +0800 CST
// path: app/controller/module/rss_api.go
// author：jon
// ==========================================================================
package module

import (
	"gin-sns/app/ginframe/response"
	"gin-sns/app/ginframe/tools"
	"gin-sns/app/model"
	"gin-sns/app/model/constants"
	followModel "gin-sns/app/model/module/follow"
	rssModel "gin-sns/app/model/module/rss"
	"gin-sns/app/service/module"

	"github.com/gin-gonic/gin"
	//"gin-sns/app/ginframe/utils/gconv"
)

var RssApi = newRssApi()

func newRssApi() *rssApi {
	return &rssApi{}
}

type rssApi struct {
}

//列表分页数据
func (c *rssApi) ListAjax(ctx *gin.Context) {
	req := new(rssModel.SelectPageReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).Log("rss管理", req).WriteJsonExit()
		return
	}
	rows := make([]rssModel.Entity, 0)
	result, page, err := module.RssService.SelectListByPage(req)

	if err == nil && len(result) > 0 {
		rows = result
	}

	response.BuildTable(ctx, page.Total, rows).WriteJsonExit()
}

//新增页面保存
func (c *rssApi) AddSave(ctx *gin.Context) {
	req := new(rssModel.AddReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Add).SetMsg(err.Error()).Log("订阅新增数据", req).WriteJsonExit()
		return
	}

	id, err := module.RssService.AddSave(req, ctx)

	if err != nil || id <= 0 {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Add).Log("订阅新增数据", req).WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetData(id).Log("订阅新增数据", req).WriteJsonExit()
}

//修改页面保存
func (c *rssApi) EditSave(ctx *gin.Context) {
	req := new(rssModel.EditReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("订阅修改数据", req).WriteJsonExit()
		return
	}

	rs, err := module.RssService.EditSave(req, ctx)

	if err != nil || rs <= 0 {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Edit).Log("订阅修改数据", req).WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetBtype(model.Buniss_Edit).Log("订阅修改数据", req).WriteJsonExit()
}

//删除数据
func (c *rssApi) Remove(ctx *gin.Context) {
	req := new(model.RemoveReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Del).SetMsg(err.Error()).Log("订阅删除数据", req).WriteJsonExit()
		return
	}

	rs := module.RssService.DeleteRecordByIds(req.Ids)

	if rs > 0 {
		response.SucessResp(ctx).SetBtype(model.Buniss_Del).Log("订阅删除数据", req).WriteJsonExit()
	} else {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Del).Log("订阅删除数据", req).WriteJsonExit()
	}
}

//GET /{id}
func (c *rssApi) GetBy(ctx *gin.Context) {
	rssId := ctx.GetInt64("id")
	rss, _ := module.RssService.SelectRecordById(rssId)
	if rss == nil {
		response.ErrorResp(ctx).SetMsg("rss invalid").WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetData(rss).WriteJsonExit()
	return
}

//GET /category/{category}
func (c *rssApi) GetCategory(ctx *gin.Context) {

	req := new(rssModel.SelectPageReq)

	req.Category = ctx.GetString("category") //todo
	req.PageNum = ctx.GetInt("page")
	req.PageSize = ctx.GetInt("size")

	rows := make([]rssModel.Entity, 0)
	result, page, err := module.RssService.SelectListByPage(req)

	if err == nil && len(result) > 0 {
		rows = result
	}

	response.BuildTable(ctx, page.Total, rows).WriteJsonExit()
}

//GET /all?page=?
func (c *rssApi) GetAll(ctx *gin.Context) {
	req := new(rssModel.SelectPageReq)

	req.PageNum = ctx.GetInt("page")
	req.PageSize = ctx.GetInt("size")

	rows := make([]rssModel.Entity, 0)
	result, page, err := module.RssService.SelectListByPage(req)

	if err == nil && len(result) > 0 {
		rows = result
	}

	response.BuildTable(ctx, page.Total, rows).WriteJsonExit()
}

//GET /user?page=?
func (c *rssApi) GetUser(ctx *gin.Context) {
	req := new(rssModel.SelectPageReq)

	user := module.UsertokenService.GetCurrent(ctx)
	if user == nil {
		response.ErrorResp(ctx).SetMsg(tools.ErrorNotLogin.Error()).WriteJsonExit()
	}

	req.UserId = user.Id
	req.PageNum = ctx.GetInt("page")
	req.PageSize = ctx.GetInt("size")

	rows := make([]rssModel.Entity, 0)
	result, page, err := module.RssService.SelectListByPage(req)

	if err == nil && len(result) > 0 {
		rows = result
	}

	response.BuildTable(ctx, page.Total, rows).WriteJsonExit()
}

//POST /create //{json}
func (c *rssApi) PostCreate(ctx *gin.Context) {
	user := module.UsertokenService.GetCurrent(ctx)
	if user == nil {
		response.ErrorResp(ctx).SetMsg(tools.ErrorNotLogin.Error()).WriteJsonExit()
		return
	}

	t := &rssModel.AddReq{} //todo Category分类
	err := ctx.ShouldBind(t)
	if err != nil {
		response.ErrorResp(ctx).SetMsg((err.Error())).WriteJsonExit()
		return
	}

	_, err = module.RssService.AddSave(t, ctx)
	if err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetData(t).WriteJsonExit()
	return
}

func (c *rssApi) PostAdd(ctx *gin.Context) {
	user := module.UsertokenService.GetCurrent(ctx)
	if user == nil {
		response.ErrorResp(ctx).SetMsg((tools.ErrorNotLogin.Error())).WriteJsonExit()
		return
	}

	t := &rssModel.AddReq{} //todo Category分类
	t.UserId = user.Id
	err := ctx.ShouldBind(t)
	if err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).WriteJsonExit()
		return
	}

	_, err = module.RssService.AddSave(t, ctx)
	if err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).WriteJsonExit()
		return
	}

	response.SucessResp(ctx).SetData(true).WriteJsonExit()
	return
}

//POST /update //{json}
func (c *rssApi) PostUpdate(ctx *gin.Context) {

	user := module.UsertokenService.GetCurrent(ctx)
	if user == nil {
		response.ErrorResp(ctx).SetMsg((tools.ErrorNotLogin.Error())).WriteJsonExit()
		return
	}

	t := &rssModel.EditReq{} //todo Category分类
	err := ctx.ShouldBind(t)
	if err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).WriteJsonExit()
		return
	}

	_, err = module.RssService.EditSave(t, ctx)
	if err != nil {
		response.ErrorResp(ctx).SetMsg("entity not found").WriteJsonExit()
		return
	}

	response.SucessResp(ctx).SetData(t).WriteJsonExit()
	return
}

//Follow
func (c *rssApi) PostFollow(ctx *gin.Context) {

	user := module.UsertokenService.GetCurrent(ctx)
	if user == nil {
		response.ErrorResp(ctx).SetMsg(tools.ErrorNotLogin.Error()).WriteJsonExit()
		return
	}

	rssId := ctx.GetInt64("rssId")
	t := &followModel.AddReq{}
	t.EntityId = rssId
	t.EntityType = constants.EntityTypeRss
	t.UserId = user.Id

	_, err := module.FollowService.AddSave(t, ctx)
	if err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).WriteJsonExit()
	}
	module.RssService.IncrFollowCount(rssId)

	response.SucessResp(ctx).SetData(t).WriteJsonExit()
	return
}

//unfollow
func (c *rssApi) PostUnfollow(ctx *gin.Context) {

	user := module.UsertokenService.GetCurrent(ctx)
	if user == nil {
		response.ErrorResp(ctx).SetMsg(tools.ErrorNotLogin.Error()).WriteJsonExit()
		return
	}

	rssId := ctx.GetInt64("rssId")
	t := &followModel.SelectPageReq{}
	t.EntityId = rssId
	t.EntityType = constants.EntityTypeRss
	t.UserId = user.Id

	rsses, err := module.FollowService.SelectListAll(t)
	if err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).WriteJsonExit()
		return
	}

	for _, rss := range rsses {
		module.FollowService.DeleteRecordById(rss.Id)
	}

	module.RssService.DecrFollowCount(rssId)

	response.SucessResp(ctx).SetData(t).WriteJsonExit()
	return
}
